var yongHus = [];
var yongHuId = 0;
for(var i = 0; i < 1086; i++) {
	yongHuId++;
	yongHus.push(suiJiYongHu(yongHuId));
}

/**
 * 通过id得到对象在数组的下标
 * a为对象数组，并且按属性id从小到大的顺序排列（二分法）
 * 例如：[{id:2},{id:3},{id:5}]
 * 
 * @param 数组 a
 * @param id id
 */
function findIndexById(a, id) {
	var l = 0;
	var r = a.length - 1;
	var m;
	while(l <= r) {
		m = (l + r) >> 1;
		if(id < a[m].id) {
			r = m - 1;
		} else if(a[m].id < id) {
			l = m + 1;
		} else {
			return m;
		}
	}
	return -1;
}

var vm = new Vue({
	el: '#app',
	data: function() {
		return {
			yongHus: [],
			tableData: [],
			index: 1,
			multipleSelection: [],
			total: 0,
			currentPage: 1,
			pageSize: 10,
			dialogEditVisible: false,
			dialogCreateVisible: false,
			bianJiYongHu0: {},
			xinZengYongHu0: {},
			kaiShiRiQi: '',
			jieShuRiQi: '',
			diZhi: '',
			xingMing: '',
			options: []
		};
	},
	computed: {
		bianJiYongHu: function() {
			var sf = jieXiShenFenZhengHao(this.bianJiYongHu0.sfzh);
			this.bianJiYongHu0.dz = sf.diZhi;
			this.bianJiYongHu0.sr = sf.shengRi;
			this.bianJiYongHu0.xb = sf.xingBie;
			this.bianJiYongHu0.yys = shouJiYunYingShang(this.bianJiYongHu0.sjh);
			return this.bianJiYongHu0;
		},
		xinZengYongHu: function() {
			var sf = jieXiShenFenZhengHao(this.xinZengYongHu0.sfzh);
			this.xinZengYongHu0.dz = sf.diZhi;
			this.xinZengYongHu0.sr = sf.shengRi;
			this.xinZengYongHu0.xb = sf.xingBie;
			this.xinZengYongHu0.yys = shouJiYunYingShang(this.xinZengYongHu0.sjh);
			return this.xinZengYongHu0;
		}
	},
	methods: {
		handleSelectionChange: function(val) {
			this.multipleSelection = val;
		},
		handleSelectionDelete: function() {
			var _this = this;
			this.$confirm('是否确认批量删除' + this.multipleSelection.length + '条数据？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function() {
				for(var i = 0; i < _this.multipleSelection.length; i++) {
					yongHus.splice(findIndexById(yongHus, _this.multipleSelection[i].id), 1);
				}
				_this.chaXun();
				_this.$message({
					type: 'success',
					message: '删除成功!'
				});
			}).catch(function() {
				_this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
		},
		handleEdit: function(row) {
			this.bianJiYongHu0 = JSON.parse(JSON.stringify(row));
			this.dialogEditVisible = true;
		},
		update: function() {
			yongHus[findIndexById(yongHus, this.bianJiYongHu.id)] = JSON.parse(JSON.stringify(this.bianJiYongHu));
			this.dialogEditVisible = false;
			this.chaXun();
			this.$message({
				type: 'success',
				message: '修改成功!'
			});
		},
		handleCreate: function() {
			this.dialogCreateVisible = true;
		},
		create: function() {
			var yongHu = JSON.parse(JSON.stringify(this.xinZengYongHu));
			this.xinZengYongHu0 = {};
			yongHuId++;
			yongHu.id = yongHuId;
			yongHus.push(yongHu);
			this.dialogCreateVisible = false;
			this.chaXun();
			this.$message({
				type: 'success',
				message: '新增成功!'
			});
		},
		handleDelete: function(index, row) {
			var _this = this;
			this.$confirm('是否确认删除第' + (index + 1) + '行“' + row.xm + '”吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function() {
				yongHus.splice(findIndexById(yongHus, row.id), 1);
				_this.chaXun();
				_this.$message({
					type: 'success',
					message: '删除成功!'
				});
			}).catch(function() {
				_this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
		},
		handleCurrentChange: function(val) {
			var n = (val - 1) * this.pageSize;
			this.index = n + 1;
			this.tableData = this.yongHus.slice(n, n + this.pageSize);
		},
		chaXun: function() {
			var _this = this;
			this.yongHus = [];
			var n = -1;
			if(_this.diZhi || _this.kaiShiRiQi || _this.jieShuRiQi || _this.xingMing) {
				while(true) {
					n = yongHus.findIndex(function(value, index, arr) {
						var b = index > n && value.sfzh && value.xm;
						if(_this.diZhi) {
							b = b && value.sfzh.substr(0, 2) == _this.diZhi;
						}
						if(_this.kaiShiRiQi) {
							b = b && new Date(value.sr) > _this.kaiShiRiQi;
						}
						if(_this.jieShuRiQi) {
							b = b && new Date(value.sr) < _this.jieShuRiQi;
						}
						if(_this.xingMing) {
							b = b && value.xm.includes(_this.xingMing);
						}
						return b;
					});
					if(n == -1) {
						break;
					}
					this.yongHus.push(yongHus[n]);
				}
			} else {
				this.yongHus = yongHus;
			}
			this.total = this.yongHus.length;
			this.handleCurrentChange(this.currentPage);
		}
	},
	mounted: function() {
		this.$nextTick(function() {
			for(var i = 0; i < shengHao.length; i++) {
				this.options.push({
					value: shengHao[i],
					label: shengMing[i]
				});
			}
			this.yongHus = JSON.parse(JSON.stringify(yongHus));
			this.total = yongHus.length;
			this.handleCurrentChange(1);
		});
	}
});